import 'package:get/get.dart';
import 'package:yysk_app/model/market_model/market_incubation_list_model.dart';
import 'package:yysk_app/model/market_model/market_incubation_time_model.dart';
import 'package:yysk_app/net/api/market_url.dart';
import 'package:yysk_app/net/http.dart';
import 'package:yysk_app/utils/js_tool.dart';

class MarketController extends GetxController {
  var number = 10.obs;
  int pageNum = 1;
  int pageSize = 10;
  final listMarket = RxList<MarketIncubationListModelItem>([]);
 final listMarketTime = Rx<MarketIncubationTimeModel?>(null);
  @override
  void onInit() {
    super.onInit();
    consoleLog('市场控制器');

    getTime().then(
      (res){
        getListData();
      }
    );
  }

  @override
  void onClose() {
    super.onClose();
  }

  getListData({loadMore = false}) {
    if (loadMore) {
      pageNum++;
    } else {
      pageNum = 1;
    }

    try {
      http.netFetch<MarketIncubationListModel>(
        MarketUrl.getCollectionPage,
        params: {'pageNum': pageNum, 'pageSize': 20},
        method: Method.post,
        success: (result) {
          List<MarketIncubationListModelItem> items = result.data?.list ?? [];
          if (loadMore && items.isEmpty) {
            pageNum--;
          }
          if (!loadMore) {
            listMarket.clear();
          }
          listMarket.addAll(items);
        },
      );
    } catch (e) {
      if (loadMore) {
        pageNum--;
      }
    }
  }
  Future<void> getTime()async {
    http.netFetch<MarketIncubationTimeModel>(
      MarketUrl.getIncubationTime,
      method: Method.get,
      success: (result) {
        listMarketTime.value =result.data;
        consoleLog('${result.data!.buyEndTime}-----9999');
        // count.value = result.data!.count ?? 0;
        //   showBtm = result.data!.supplyTransferable!;
      },
    );
  }
}
